# @Time : 2021/4/13 14:53
# @Author : Fioman 
# @Phone : 13149920693
"""
和前端通信的工具接口,这个模块主要是用来发送前端消息的.
"""
import json

from db_tools.db_handler import DbHandler
from tools.websocket_queue import WebSocketQueue

msgQueue = WebSocketQueue().websocketQueue
db = DbHandler()


def send_login_usernames_to_ui(msg):
    msgType = "loginUsernameState"
    send_msg_to_ui(msgType, msg)


def send_big_cutter_camera_cal_state(msg):
    """
    更新铡刀标定的时候的使用到的变量信息
    :param msg: 消息内容
    :return:
    """
    msgType = "BigCutterCamCalState"
    send_msg_to_ui(msgType, msg)


def send_small_cutter_camera_cal_state(msg):
    """
    更新铡刀标定的时候的使用到的变量信息
    :param msg: 消息内容
    :return:
    """
    msgType = "SmallCutterCamCalState"
    send_msg_to_ui(msgType, msg)


def send_line_camera_joint_state(msg):
    """
    更新线阵相机的拼接界面的参数信息.
    :param msg: 消息内容
    :return:
    """
    msgType = "lineCamJointState"
    send_msg_to_ui(msgType, msg)


def send_line_camera_cal_state_to_ui(msg):
    """
    更新线阵相机的界面,主要用来刷新这个界面的状态更新
    :param msg: 消息内容
    :return:
    """
    msgType = "lineCamCalState"
    send_msg_to_ui(msgType, msg)


def send_cbj_zmc_tool_state_to_ui(msg):
    """
    更新裁切机工具界面的状态信息
    :param msg 要发送的消息
    :return:
    """
    msgType = "cbjZmcToolState"
    send_msg_to_ui(msgType, msg)


def send_settings_update_to_ui():
    """
    更新设置界面的数据的状态
    :return:
    """
    msgType = 'settingsUpdate'
    msg = {
        "val": "1"
    }
    send_msg_to_ui(msgType, msg)


def send_working_info_state_to_ui():
    """
    更新workinginfo的状态,这里
    :return:
    """
    msgType = "produceState"
    msg = {
        "val": 'workingInfo'
    }
    send_msg_to_ui(msgType, msg)


def send_produce_state_to_ui(msgVal):
    """
    发送生产作业的页面的状态信息
    :param msgVal 消息内容
    :return:
    """
    msgType = "produceState"
    send_msg_to_ui(msgType, msgVal)


def update_msg_send(new_val):
    """
    更新界面上send的值
    @param new_val:
    @return:
    """
    msgType = "eap_msg_state"
    msgVal = {
        "val": "updateMsgSend",
        "newVal": new_val
    }
    send_msg_to_ui(msgType, msgVal)


def update_msg_receive(new_val):
    """
    更新界面上receive的值
    @param new_val:
    @return:
    """
    msgType = "eap_msg_state"
    msgVal = {
        "val": "updateMsgReceive",
        "newVal": new_val
    }
    send_msg_to_ui(msgType, msgVal)


def update_msg_response(new_val):
    """
    更新界面上response的值
    @param new_val:[result,return_code,return_msg]
    @return:
    """
    msgType = "eap_msg_state"
    msgVal = {
        "val": "updateResponse",
        "newVal": new_val
    }
    send_msg_to_ui(msgType, msgVal)


def update_eap_status_to_ui(newVal):
    """
    更新界面上EapStatus eap是否在线
    @param newVal
    @return:
    """
    msgType = "eap_msg_state"
    msgVal = {
        "val": "updateEapStatus",
        "newVal": newVal
    }
    send_msg_to_ui(msgType, msgVal)


def update_current_user_id(new_val):
    """
    更新界面上当前登录的用户Id
    @param new_val:
    @return:
    """
    msgType = "eap_msg_state"
    msgVal = {
        "val": "updateEapUserId",
        "newVal": new_val
    }
    send_msg_to_ui(msgType, msgVal)


def update_eap_config_to_ui(newVal: list):
    """
    这里的newVal要是一个列表,原因是对应的UI界面的table,虽然只有一条数据,也要返回集合的形式
    @param newVal:
    @return:
    """
    msgType = "eap_msg_state"
    msgVal = {
        "val": "updateEapParamsState",
        "newVal": newVal
    }
    send_msg_to_ui(msgType, msgVal)


def send_eap_msg_state_to_ui(msgVal):
    """
    发送eap页面的状态消息给前端
    :param msgVal 消息内容
    """
    msgType = "eap_msg_state"
    send_msg_to_ui(msgType, msgVal)


def send_order_state_to_ui(msg):
    """
    更新工单状态
    :param msg:使用的key值
    :return:
    """
    msgType = "orderStates"
    send_msg_to_ui(msgType, msg)


def send_msg_to_ui(msgType, msg):
    """
    将websocketQueue的内容发送给前端.
    :param msgType: 消息类型
    :param msg: 消息内容
    :return:
    """
    msgQueue.put(json.dumps({msgType: msg}))


def update_cim_message(messageType, cimMessage, timeStamp, showTimeLeft):
    """
    更新显示的消息
    @param cimMessage:
    @return:
    """
    msgType = "eap_msg_state"
    if cimMessage is None or cimMessage == ''"":
        msgVal = {
            "val": "updateCimMessage",
            "visible": False,
            "messageType": "",
            "cimMessage": "",
            "timeStamp": "",
            "showTimeLeft": "",
        }
    else:
        msgVal = {
            "val": "updateCimMessage",
            "visible": True,
            "messageType": messageType,
            "cimMessage": cimMessage,
            "timeStamp": timeStamp,
            "showTimeLeft": showTimeLeft,
        }
    send_msg_to_ui(msgType, msgVal)


def update_prodinfo_download_to_ui():
    res, data = db.get_production_download_info_data()
    if res == "ok":
        newVal = data
    else:
        newVal = []
    msgType = "eap_msg_state"
    msgVal = {
        "val": "updateProdInfoDownload",
        "newVal": newVal
    }
    send_msg_to_ui(msgType, msgVal)


def update_lot_command_download_to_ui():
    res, data = db.get_lot_command_download_info()
    if res == "ok":
        newVal = data
    else:
        newVal = []
    msgType = "eap_msg_state"
    msgVal = {
        "val": "updateLotCommandDownload",
        "newVal": newVal
    }
    send_msg_to_ui(msgType, msgVal)
